home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / util2 / pgp22src.zip / SRC / PASSWD.C < prev    next >
C/C++ Source or Header  |  1993-03-07  |  2KB  |  79 lines

  1. /*    passwd.c - Password reading/hashing routines
  2.     (c) 1989 Philip Zimmermann.  All rights reserved.
  3.     Implemented in Microsoft C.
  4.     Routines for getting a pass phrase from the user's console.
  5. */
  6.  
  7. #include    <stdio.h>    /* for fprintf() */
  8. #include    <ctype.h>    /* for isdigit(), toupper(), etc. */
  9. #include    <string.h>    /* for strlen() */
  10.  
  11. #include    "random.h"    /* for getstring() */
  12. #include    "md5.h"
  13. #include    "language.h"
  14. #include    "pgp.h"
  15.  
  16. #define MAXKEYLEN 254    /* max byte length of pass phrase */
  17.  
  18. boolean showpass = FALSE;
  19.  
  20. /*
  21. **    hashpass - Hash pass phrase down to 128 bits (16 bytes).
  22. **  keylen must be less than 1024.
  23. **    Use the MD5 algorithm.
  24. */
  25. void hashpass (char *keystring, int keylen, byte *hash)
  26. {
  27.     MD5_CTX    mdContext;
  28.  
  29.     /* Calculate the hash */
  30.     MD5Init(&mdContext);
  31.     MD5Update(&mdContext, (unsigned char *) keystring, keylen);
  32.     MD5Final(&mdContext);
  33.     /* Copy it to return variable */
  34.     memcpy(hash, mdContext.digest, 16);
  35. }    /* hashpass */
  36.  
  37.  
  38. /*
  39. **    GetHashedPassPhrase - get pass phrase from user, hashes it to an IDEA key.
  40.     Parameters:
  41.         returns char *keystring as the pass phrase itself
  42.         return char *hash as the 16-byte hash of the pass phrase
  43.                 using MD5.
  44.         byte noecho:  
  45.             0=ask once, echo. 
  46.             1=ask once, no echo. 
  47.             2=ask twice, no echo.
  48.     Return 0 if no characters are input, else return 1.
  49.     If we return 0, the hashed key will not be useful.
  50. */
  51. int GetHashedPassPhrase(char *keystring, char *hash, boolean noecho)
  52. {    char keystr2[MAXKEYLEN+2];
  53.     int len;
  54.  
  55.     if (showpass)
  56.         noecho = 0;
  57.     while (TRUE) {
  58.         fprintf(pgpout,PSTR("\nEnter pass phrase: "));
  59.         getstring(keystring,MAXKEYLEN-1,!noecho);
  60.         if (noecho<2)    /* no need to ask again if user can see it */
  61.             break;
  62.         fprintf(pgpout,PSTR("\nEnter same pass phrase again: "));
  63.         getstring(keystr2,MAXKEYLEN-1,!noecho);
  64.         if (strcmp(keystring,keystr2)==0)
  65.             break;
  66.         fprintf(pgpout,PSTR("\n\007Error: Pass phrases were different.  Try again."));
  67.     }
  68.     if (noecho && (filter_mode || quietmode))
  69.         putc('\n', pgpout);
  70.  
  71.     len = strlen(keystring);
  72.     if (len == 0)
  73.         return 0;
  74.     /* We assume ASCII pass phrases, with no charset conversions. */
  75.     hashpass (keystring, strlen(keystring), (byte *) hash);
  76.     return 1;
  77. }    /* GetHashedPassPhrase */
  78.  
  79.